home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_008 / src / hack.o_init.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  4KB  |  137 lines

  1. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1984. */
  2.  
  3. #include   "config.h"      /* for typedefs */
  4. #include   "def.objects.h"
  5. extern char ismklev;
  6.  
  7. int
  8. letindex(let) register char let; {
  9. register int i = 0;
  10. register char ch;
  11.    while((ch = obj_symbols[i++]) != 0)
  12.       if(ch == let) return(i);
  13.    return(0);
  14. }
  15.  
  16. init_objects(){
  17. register int i, j, first, last, sum, end;
  18. register char let, *tmp;
  19.    /* init base; if probs given check that they add up to 100, 
  20.       otherwise compute probs; shuffle descriptions */
  21.    end = sizeof(objects)/sizeof(objects[0]);
  22.    first = 0;
  23.    while( first < end ) {
  24.       let = objects[first].oc_olet;
  25.       last = first+1;
  26.       while(last < end && objects[last].oc_olet == let
  27.             && objects[last].oc_name != NULL)
  28.          last++;
  29.       i = letindex(let);
  30.       if((!i && let != ILLOBJ_SYM) || bases[i] != 0)
  31.          panic("initialization error");
  32.       bases[i] = first;
  33.    check:
  34. #include   "hack.onames.h"
  35.       if(ismklev && let == GEM_SYM) {
  36.          extern xchar dlevel;
  37.          for(j=0; j < 9-dlevel/3; j++)
  38.             objects[first+j].oc_prob = 0;
  39.          first += j;
  40.          if(first >= last || first >= LAST_GEM)
  41.             printf("Not enough gems? - first=%d last=%d j=%d LAST_GEM=%d\n", first, last, j, LAST_GEM);
  42.          for(j = first; j < LAST_GEM; j++)
  43.              objects[j].oc_prob = (20+j-first)/(LAST_GEM-first);
  44.       }
  45.       sum = 0;
  46.       for(j = first; j < last; j++) sum += objects[j].oc_prob;
  47.       if(sum == 0) {
  48.          for(j = first; j < last; j++)
  49.              objects[j].oc_prob = (100+j-first)/(last-first);
  50.          goto check;
  51.       }
  52.       if(sum != 100)
  53.          panic ("init-prob error for %c", let);
  54.       /* shuffling is rather meaningless in mklev, 
  55.          but we must update  last  anyway */
  56.       if(objects[first].oc_descr != NULL && let != TOOL_SYM){
  57.          /* shuffle, also some additional descriptions */
  58.          while(last < end && objects[last].oc_olet == let)
  59.             last++;
  60.          j = last;
  61.          while(--j > first) {
  62.             i = first + rn2(j+1-first);
  63.             tmp = objects[j].oc_descr;
  64.             objects[j].oc_descr = objects[i].oc_descr;
  65.             objects[i].oc_descr = tmp;
  66.          }
  67.       }
  68.       first = last;
  69.    }
  70. }
  71.  
  72. probtype(let) register char let; {
  73. register int i = bases[letindex(let)];
  74. register int prob = rn2(100);
  75.    while((prob -= objects[i].oc_prob) >= 0) i++;
  76.    if(objects[i].oc_olet != let || !objects[i].oc_name)
  77.       panic("probtype(%c) error, i=%d", let, i);
  78.    return(i);
  79. }
  80.  
  81. #define SIZE(x) (sizeof x)/(sizeof x[0])
  82. extern long *alloc();
  83.  
  84. savenames(fd) register int fd; {
  85. register int i;
  86. unsigned len;
  87.     bwrite(fd, (char *) bases, sizeof bases);
  88.     bwrite(fd, (char *) objects, sizeof objects);
  89.     /* as long as we use only one version of Hack/Quest we
  90.         need not save oc_name and oc_descr, but we must save
  91.         oc_uname for all objects */
  92.     /* this assumes we always load at the same place */
  93.     for(i=0; i < SIZE(objects); i++) {
  94. #ifdef AMIGA
  95.         if(objects[i].oc_name) {
  96.             len = strlen(objects[i].oc_name)+1;
  97.             bwrite(fd, (char *) &len, sizeof len);
  98.             bwrite(fd, objects[i].oc_name, len);
  99.         }
  100.         if(objects[i].oc_descr) {
  101.             len = strlen(objects[i].oc_descr)+1;
  102.             bwrite(fd, (char *) &len, sizeof len);
  103.             bwrite(fd, objects[i].oc_descr, len);
  104.         }
  105. #endif
  106.         if(objects[i].oc_uname) {
  107.             len = strlen(objects[i].oc_uname)+1;
  108.             bwrite(fd, (char *) &len, sizeof len);
  109.             bwrite(fd, objects[i].oc_uname, len);
  110.         }
  111.     }
  112. }
  113.  
  114. restnames(fd) register int fd; {
  115. register int i;
  116. unsigned len;
  117.     mread(fd, (char *) bases, sizeof bases);
  118.     mread(fd, (char *) objects, sizeof objects);
  119.     for(i=0; i < SIZE(objects); i++) {
  120.         if(objects[i].oc_name) {
  121.             mread(fd, (char *) &len, sizeof len);
  122.             objects[i].oc_name = (char *) alloc(len);
  123.             mread(fd, objects[i].oc_name, len);
  124.         }
  125.         if(objects[i].oc_descr) {
  126.             mread(fd, (char *) &len, sizeof len);
  127.             objects[i].oc_descr = (char *) alloc(len);
  128.             mread(fd, objects[i].oc_descr, len);
  129.         }
  130.         if(objects[i].oc_uname) {
  131.             mread(fd, (char *) &len, sizeof len);
  132.             objects[i].oc_uname = (char *) alloc(len);
  133.             mread(fd, objects[i].oc_uname, len);
  134.         }
  135.     }
  136. }
  137.